package com.ymatou.iapi.sellercs.testcase;

import java.io.IOException;
import java.util.ArrayList;

import org.apache.http.client.ClientProtocolException;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.google.gson.JsonObject;
import com.ymatou.iapi.sellercs.parameter.GetCsByMasterCsIdBean;
import com.ymatou.iapi.sellercs.parameter.OnlineCS;
import com.ymatou.iapi.sellercs.parameter.SendMessageBean;
import com.ymatou.iapi.sellercs.service.GetCsByMasterCsIdCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.business.service.SellerCSIMCallService;
import com.ymttest.database.sqlwapper.YmtSellerCSIMWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*获取子客服列表
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GetCsByMasterCsId{
	private static GetCsByMasterCsIdBean getcsbymastercsidBean;
	private static GetCsByMasterCsIdCall getcsbymastercsidCall;
	YmtSellerCSIMWapper YmtSellerCSIMWapper;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取子客服列表");
	}

	@Before
	public void caseUp() throws ClientProtocolException, IOException {
		getcsbymastercsidBean=new GetCsByMasterCsIdBean();
		getcsbymastercsidCall=new GetCsByMasterCsIdCall();
		YmtSellerCSIMWapper=new YmtSellerCSIMWapper();
		SellerCSIMCallService.OffLine(sellerId, null);
		SellerCSIMCallService.OffLine(zsellerId, null);
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getcsbymastercsidCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCsByMasterCsId_001() {
		Logger.start(true,"获取子客服列表成功,子客服在线");
		try {
			SellerCSIMCallService.AppLogin(sellerId);
			getcsbymastercsidBean.setMasterCSId(sellerId);
			getcsbymastercsidCall.setData(getcsbymastercsidBean);
			getcsbymastercsidCall.callService();			
			Logger.verifyIsContains("200", getcsbymastercsidCall.getString("Status"), "验证Status");
			Logger.debug(FormatUtil.beanToGSONString(getcsbymastercsidCall.getResult()));
			ArrayList<OnlineCS> list = getcsbymastercsidCall.getResult();
			Logger.verifyEquals(1, list.size(),"验证1个用户在线");
			Logger.verifyEquals(sellerId,list.get(0).getCSId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(sellerId).getLoginId(),list.get(0).getCSName(), "验证csname");
			Logger.verifyEquals(0,list.get(0).getCurrentSessions(), "验证会话数");
			Logger.verifyEquals(0,list.get(0).getStatus(), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_GetCsByMasterCsId_002() {
		Logger.start(true,"不输入sellerid失败");
		try {
			getcsbymastercsidCall.setData(getcsbymastercsidBean);
			getcsbymastercsidCall.callService();			
			Logger.verifyIsContains("400", getcsbymastercsidCall.getString("Status"), "验证Status");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsByMasterCsId_003() {
		Logger.start(true,"获取获取子客服列表成功,子客服离线");
		try {
			SellerCSIMCallService.AppLogin(sellerId);
			SellerCSIMCallService.LeaveLine(sellerId);
			getcsbymastercsidBean.setMasterCSId(sellerId);
			getcsbymastercsidCall.setData(getcsbymastercsidBean);
			getcsbymastercsidCall.callService();			
			Logger.verifyIsContains("200", getcsbymastercsidCall.getString("Status"), "验证Status");
			Logger.debug(FormatUtil.beanToGSONString(getcsbymastercsidCall.getResult()));
			ArrayList<OnlineCS> list = getcsbymastercsidCall.getResult();
			Logger.verifyEquals(1, list.size(),"验证1个用户在线");
			Logger.verifyEquals(sellerId,list.get(0).getCSId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(sellerId).getLoginId(),list.get(0).getCSName(), "验证csname");
			Logger.verifyEquals(0,list.get(0).getCurrentSessions(), "验证会话数");
			Logger.verifyEquals(1,list.get(0).getStatus(), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_GetCsByMasterCsId_004() {
		Logger.start(true,"获取获取子客服列表成功,子客服有正在进行会话");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);

			JsonObject registerResult = urrc.getJsonObject().get("Result").getAsJsonObject();
			String token = registerResult.get("Token").getAsString();
			Integer userId = urrc.getUserid();
			
			SellerCSIMCallService.AppLogin(sellerId);
			SendMessageBean sendmessageBean = SellerCSIMCallService.initSendmessageBean(userId, sellerId, "connect");
			SellerCSIMCallService.SendMessage(sendmessageBean);
			// 触发分配
			SellerCSIMCallService.Assign();
			Thread.sleep(3000);
			
			getcsbymastercsidBean.setMasterCSId(sellerId);
			getcsbymastercsidCall.setData(getcsbymastercsidBean);
			getcsbymastercsidCall.callService();			
			Logger.verifyIsContains("200", getcsbymastercsidCall.getString("Status"), "验证Status");
			Logger.debug(FormatUtil.beanToGSONString(getcsbymastercsidCall.getResult()));
			ArrayList<OnlineCS> list = getcsbymastercsidCall.getResult();
			Logger.verifyEquals(1, list.size(),"验证1个用户在线");
			Logger.verifyEquals(sellerId,list.get(0).getCSId(), "验证csid");
			Logger.verifyEquals(JUserCallService.GetUserInfo(sellerId).getLoginId(),list.get(0).getCSName(), "验证csname");
			Logger.verifyEquals(1,list.get(0).getCurrentSessions(), "验证会话数");
			Logger.verifyEquals(0,list.get(0).getStatus(), "验证Status");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}